﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace AutobuildAddIn
{
    public class Functional
    {
    #region TortoiseSVN
        // ' TortoiseProc.exe /command:diff /path:""" & filepath & """ /startrev:" & rev1.ToString & " /endrev:" & rev2.ToString & " /closeonend:0
        public void tortoise_diff(string filepath, int rev1, int rev2)
        {
            
            Process p = new Process();
            p.StartInfo.FileName = "TortoiseProc.exe";
            p.StartInfo.Arguments = ("/command:diff /path:\""
                        + (filepath + ("\" /startrev:"
                        + (rev1.ToString + (" /endrev:"
                        + (rev2.ToString + " /closeonend:0"))))));
            p.StartInfo.UseShellExecute = false;
            p.Start();
            p.Close();
        } 
        //  TortoiseProc.exe /command:diff /path:PATH /startrev:REV1 /endrev:REV2 /closeonend:0
        public void tortoise_export()
        {
        }
#endregion

    #region Subversion
        //  svn diff -r REV1:REV2 path
        public string svn_diff(int rev1, int rev2, string file)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            IO.StreamWriter sw = p.StandardInput;
            sw.WriteLine(("diff -r "
                            + (rev1.ToString + (":"
                            + (rev2.ToString + (" " + file))))));
            sw.Close();
            string output = p.StandardOutput.ReadToEnd;
            p.WaitForExit();
            p.Close();
            return output;
        }
        // svn export -r REV URLsvnpath build path
        public void svn_export()
        {
        }
#endregion

    #region Windows
        public bool MoveFile(string File, string From, string Too)
        {
            if ((System.IO.File.Exists((From + File)) == true))
            {
                try
                {
                    System.IO.Directory.Move((From + File), (Too + File));
                }
                catch (IO.IOException ex)
                {
                    MsgBox(("Unable to move "
                                    + (File.ToString + (" to " + Too.ToString))));
                    return false;
                }
                return true;
            }
        }
        public bool MoveDirectory(string From, string Too)
        {
            if ((System.IO.Directory.Exists(From) == true))
            {
                try
                {
                    System.IO.Directory.Move(From, Too);
                }
                catch (IO.IOException ex)
                {
                    MsgBox(("Unable to move directory "
                                    + (From.ToString + (" to " + Too.ToString))));
                    return false;
                }
                return true;
            }
        }
        public bool CopyFile(string File, string From, string Too)
        {
            if ((System.IO.File.Exists((From + File)) == false))
            {
                try
                {
                    System.IO.File.Copy((From + File), (Too + File));
                }
                catch (IO.IOException ex)
                {
                    MsgBox(("Unable to copy "
                                    + (File.ToString + (" to " + Too.ToString))));
                    return false;
                }
                return true;
            }
        }
        public bool CopyDirectory(string From, string Too)
        {
            if ((System.IO.Directory.Exists(From) == false))
            {
                try
                {
                    My.Computer.FileSystem.CopyDirectory(From, Too);
                }
                catch (Exception ex)
                {
                    MsgBox(("Unable to copy directory "
                                    + (From.ToString + (" to " + Too.ToString))));
                    return false;
                }
                return true;
            }
        }
        public bool CreateFile(string File, string path)
        {
            if ((System.IO.File.Exists((path + File)) == true))
            {
                try
                {
                    System.IO.File.Create((path + File));
                }
                catch (IO.IOException ex)
                {
                    MsgBox(("Unable to create file here:"
                                    + (path.ToString + File.ToString)));
                    return false;
                }
                return true;
            }
        }
        public bool CreateDirectory(string path)
        {
            if ((System.IO.Directory.Exists(path) == true))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                catch (IO.IOException ex)
                {
                    MsgBox(("Unable to create directory here:" + path.ToString));
                    return false;
                }
                return true;
            }
        }
        public string Commandline(string command)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            IO.StreamWriter sw = p.StandardInput;
            sw.WriteLine(command);
            sw.Close();
            string output = p.StandardOutput.ReadToEnd;
            p.WaitForExit();
            p.Close();
            return output;
        } //  Use cmd.exe
        public bool ErrorCheck(string path, string err)
        {
            IO.StreamReader sr = new IO.StreamReader(path);
            string text = sr.ReadToEnd;
            if ((text.Contains(err) == true))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
#endregion
    }
}
